package com.twt.gym.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.twt.common.consts.SysDicts;
import com.twt.common.exceptions.AppException;
import com.twt.common.pojo.Result;
import com.twt.common.utils.Assert;
import com.twt.gym.service.TGymMemberService;
import com.twt.model.member.entity.TGymMember;
import com.twt.model.member.vo.TGymMemberQueryVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * (t_gym_member)表控制层
 *
 * @author xxxxx
 */
@RestController
@RequestMapping("/member")
public class TGymMemberController {

    @Autowired
    private TGymMemberService memberService;

    @GetMapping("/query/page/{currentPage}/{pageSize}")
    public Result queryPageGymMember(@PathVariable("currentPage") long currentPage, @PathVariable("pageSize") long pageSize, TGymMemberQueryVo queryVo) {
        Page<TGymMember> page = new Page<>();
        page.setCurrent(currentPage);
        page.setSize(pageSize);

        QueryWrapper<TGymMember> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(queryVo.getName())) {
            queryWrapper.like("name", queryVo);
        }
        Page<TGymMember> memberPage = memberService.page(page, queryWrapper);
        return Result.ok(memberPage);
    }

    @GetMapping("/query/one/{memberNo}")
    public Result queryPageGymMember(@PathVariable("memberNo") String memberNo) {
        Assert.isBlank(memberNo, "memberNo 不能为空！");
        QueryWrapper<TGymMember> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("id", memberNo);

        TGymMember one = memberService.getOne(queryWrapper);
        return Result.ok(one);
    }

    @PostMapping("/add")
    public Result add(TGymMember member) {
        Assert.isBlank(member.getName(), "会员名字 不能为空！");
        Assert.isBlank(member.getTel(), "会员电话 不能为空！");

        QueryWrapper<TGymMember> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("tel", member.getTel());
        queryWrapper.eq("is_deleted", SysDicts.DELETE_NO);
        TGymMember one = memberService.getOne(queryWrapper);
        if (one != null) {
            throw new AppException(String.format("电话号码【%s】已经存在"));
        }
        memberService.save(member);
        return Result.ok();
    }

    @PutMapping("/update")
    public Result update(TGymMember member) {
        Assert.isBlank(member.getId(), "id 不能为空！");
        Assert.isBlank(member.getName(), "会员名字 不能为空");
        Assert.isBlank(member.getTel(), "会员电话 不能为空！");
        memberService.updateById(member);
        return Result.ok();
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable("id") String id) {
        Assert.isBlank(id, "id 不能为空！");
        memberService.removeById(id);
        return Result.ok();
    }

    @DeleteMapping("/deleteByIds")
    public Result deleteByIds(String ids) {
        Assert.isBlank(ids, "ids 不能为空！");
        Arrays.asList(ids.split(",")).forEach(id -> {
            memberService.removeById(id);
        });
        return Result.ok();
    }


}
